package com.redis.aopmdx;

import com.redis.aopmdx.zhujie.MdxAopApi;
import com.redis.midengxing.FreemarkerUtils;
import com.redis.midengxing.TestMiDengXingController;
import com.redis.midengxing.UID;
import com.redis.midengxing.zhujie.MdxApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * 作者: LCG
 * 日期: 2020/7/7 00:51
 * 描述:
 */

@RestController
public class TestMdxAopController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 测试幂等性
     * @return
     */
    @RequestMapping("mdx2")
    @MdxAopApi
    public String test01(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){

        System.out.println("====================="+httpServletRequest.getParameter("code"));


        redisTemplate.opsForValue().get("");
        redisTemplate.opsForValue().increment("",1);

        redisTemplate.expire("key",20,TimeUnit.SECONDS);
        return "ok";
    }

    /**
     * 获取Code值
     * @return
     */
    @RequestMapping("getCode2")
    public String testGetCode(){
        String uuid16 = UID.getUUID16();
        redisTemplate.opsForValue().set(uuid16,uuid16,1,TimeUnit.HOURS);
        return uuid16;
    }


    /**
     * 转到测试页面
     * @param httpServletRequest
     * @param httpServletResponse
     */
    @RequestMapping("toIndex2")
    public void test(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception {

        httpServletResponse.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = httpServletResponse.getWriter();
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        FreemarkerUtils.getStaticHtml(TestMiDengXingController.class,
                "/template/",
                "test2.html",stringObjectHashMap,writer);

    }
}
